/**
 * 
 */
package com.java.test.activity.mapper;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import com.java.test.activity.dto.ActivityManageInfo;
import com.java.test.util.PageInfo;

/**
 * @author Administrator
 *
 */

@Component
public class ActivityManageMapper {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	public int selectactivityCount(ActivityManageInfo info) {

		String sql = "select * from activit_manage where 1=1";

//		if (info.getStatus() != 0) {
//			sql += " and status='" + info.getStatus() + "'";
//		}

		if (info.getStartTime() != null) {
			sql += " and TO_DAYS(start_time)= TO_DAYS('" + info.getStartTime() + "')";
		}

		List<ActivityManageInfo> list = jdbcTemplate.query(sql, new ActivityManageInfo());

		list.forEach(b -> {
			LocalDateTime startTime = b.getStartTime();
			LocalDateTime endTime = b.getEndTime();
			LocalDateTime nowTime = LocalDateTime.now();

			int cura = nowTime.compareTo(startTime);
			int curb = nowTime.compareTo(endTime);

			if (cura < 0) {
				b.setStatus(1);
			} else {
				if (curb < 0) {
					b.setStatus(2);
				} else {
					b.setStatus(3);
				}
			}
		});

		if (list != null && list.size() > 0 && info.getStatus() != 0) {
			list = list.stream().filter(b -> b.getStatus() == info.getStatus()).collect(Collectors.toList());
		}

		return (list == null || list.size() <= 0) ? 0 : list.size();
	}

	public List<ActivityManageInfo> selectactivityList(ActivityManageInfo info, PageInfo<ActivityManageInfo> page) {

		String sql = "select * from activit_manage where 1=1";

//		if (info.getStatus() != 0) {
//			sql += " and status='" + info.getStatus() + "'";
//		}

		if (info.getStartTime() != null) {
			sql += " and TO_DAYS(start_time)= TO_DAYS('" + info.getStartTime() + "')";
		}

		int start = (page.getCurrentPage() - 1) * page.getPageCount();
		int limit = page.getPageCount();

		sql += " order by start_time desc limit ?,?";

		List<ActivityManageInfo> list = jdbcTemplate.query(sql, new ActivityManageInfo(), start, limit);

		return list;
	}

	public int insertActivityManageInfo(ActivityManageInfo info) {

		String sql = "insert into activit_manage(activity_name,img_url,activit_jump_url,"
				+ "start_time,end_time,status)values(?,?,?,?,?,1)";

		int i = jdbcTemplate.update(sql, info.getActivityName(), info.getImgUrl(), info.getActivitJumpUrl(),
				info.getStartTime(), info.getEndTime());
		return i;
	}

	public int updateActivityManageInfo(ActivityManageInfo info) {
		String sql = "update activit_manage set activity_name=?,img_url=?,activit_jump_url=?,"
				+ "start_time=?,end_time=? where id =?";

		int i = jdbcTemplate.update(sql, info.getActivityName(), info.getImgUrl(), info.getActivitJumpUrl(),
				info.getStartTime(), info.getEndTime(), info.getId());
		return i;
	}

	public int deleteActivityManageInfo(int[] ids) {
		String sql = "delete from  activit_manage where id=?";
		jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
			public void setValues(PreparedStatement ps, int i) throws SQLException {
				ps.setInt(1, ids[i]);
			}

			public int getBatchSize() {
				return ids.length;
			}
		});
		return 1;
	}

	public ActivityManageInfo selectactivityInfo(ActivityManageInfo info) {
		String sql = "select * from activit_manage where id=?";

		ActivityManageInfo infos = jdbcTemplate.queryForObject(sql, new ActivityManageInfo(), info.getId());

		return infos;

	}

}
